added Feb 2001 SDK
[windows-sources.git] / shared source / sscli20 / jscript / engine / customattributelist.cs
blob7fca03048781384065c21df1d6eb3b9eba458ced
1 // ==++==
2 //
3 //
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
5 //
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
10 //
11 // You must not remove this notice, or any other, from this software.
12 //
13 //
14 // ==--==
16 namespace Microsoft.JScript {
18 using System;
19 using System.Collections;
20 using System.Reflection.Emit;
22 internal sealed class CustomAttributeList : AST{
23 private ArrayList list;
24 private ArrayList customAttributes;
25 private bool alreadyPartiallyEvaluated;
27 internal CustomAttributeList(Context context)
28 : base(context) {
29 this.list = new ArrayList();
30 this.customAttributes = null;
31 this.alreadyPartiallyEvaluated = false;
34 internal void Append(CustomAttribute elem){
35 this.list.Add(elem);
36 this.context.UpdateWith(elem.context);
39 internal bool ContainsExpandoAttribute(){ //Use only before partial evaluation has been done
40 for (int i = 0, n = this.list.Count; i < n; i++){
41 CustomAttribute ca = (CustomAttribute)this.list[i];
42 if (ca == null) continue;
43 if (ca.IsExpandoAttribute()) return true;
45 return false;
48 // Assume the custom attribute list has been partially evaluated already
49 internal CustomAttribute GetAttribute(Type attributeClass){
50 for (int i = 0, n = this.list.Count; i < n; i++){
51 CustomAttribute ca = (CustomAttribute)this.list[i];
52 if (ca == null) continue;
53 Object type = ca.type;
54 if (type is Type)
55 if (type == attributeClass)
56 return (CustomAttribute)this.list[i];
58 return null;
61 internal override Object Evaluate(){
62 return this.Evaluate(false);
65 internal Object Evaluate(bool getForProperty){
66 int n = this.list.Count;
67 ArrayList attrValues = new ArrayList(n);
68 for (int i = 0; i < n; i++){
69 CustomAttribute ca = (CustomAttribute)this.list[i];
70 if (ca == null) continue;
71 if (ca.raiseToPropertyLevel){
72 if (!getForProperty) continue;
73 }else
74 if (getForProperty) continue;
75 attrValues.Add(ca.Evaluate());
77 Object[] result = new Object[attrValues.Count];
78 attrValues.CopyTo(result);
79 return result;
82 internal CustomAttributeBuilder[] GetCustomAttributeBuilders(bool getForProperty){
83 this.customAttributes = new ArrayList(this.list.Count);
84 for (int i = 0, n = this.list.Count; i < n; i++){
85 CustomAttribute ca = (CustomAttribute)this.list[i];
86 if (ca == null) continue;
87 if (ca.raiseToPropertyLevel){
88 if (!getForProperty) continue;
89 }else
90 if (getForProperty) continue;
91 CustomAttributeBuilder attribute = ca.GetCustomAttribute();
92 if (attribute != null)
93 this.customAttributes.Add(attribute);
95 CustomAttributeBuilder[] result = new CustomAttributeBuilder[this.customAttributes.Count];
96 this.customAttributes.CopyTo(result);
97 return result;
100 internal override AST PartiallyEvaluate(){
101 if (this.alreadyPartiallyEvaluated) return this;
102 this.alreadyPartiallyEvaluated = true;
103 for (int i = 0, n = this.list.Count; i < n; i++)
104 this.list[i] = ((CustomAttribute)this.list[i]).PartiallyEvaluate();
105 for (int i = 0, n = this.list.Count; i < n; i++){
106 CustomAttribute ca = (CustomAttribute)this.list[i];
107 if (ca == null) continue; //Already found to be invalid
108 Object caType = ca.GetTypeIfAttributeHasToBeUnique();
109 if (caType == null) continue;
110 for (int j = i+1; j < n; j++){
111 CustomAttribute caj = (CustomAttribute)this.list[j];
112 if (caj == null) continue;
113 if (caType == caj.type){
114 caj.context.HandleError(JSError.CustomAttributeUsedMoreThanOnce);
115 this.list[j] = null; //Remove duplicate
119 return this;
122 internal void Remove(CustomAttribute elem){
123 this.list.Remove(elem);
126 internal void SetTarget(AST target){
127 for (int i = 0, n = this.list.Count; i < n; i++)
128 ((CustomAttribute)this.list[i]).SetTarget(target);
131 internal override void TranslateToIL(ILGenerator il, Type rtype){
134 internal override void TranslateToILInitializer(ILGenerator il){